package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.ruicar.afs.cloud.afscase.channel.service.ChannelOnlineInfoService;
import com.ruicar.afs.cloud.afscase.channel.vo.ChannelCaseInfoVo;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author mingzhi.li
 * @version 1.0
 * @createDate 2020-09-25
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ChannelOnlineInfoProcessor implements AfsMqBizProcessor<ChannelCaseInfoVo> {
    private final ChannelOnlineInfoService channelOnlineInfoService;
    @Override
    public MqTransCode getCode() {
        return MqTransCode.CHANNEL_ONLINE_DEALER_INFO__TO_CASE;
    }

    @Override
    public boolean processMessage(ChannelCaseInfoVo vo) throws Exception {
        this.ChannelOnlineInfo(vo);
        return true;
    }

    @Transactional(rollbackFor = Exception.class)
    public String ChannelOnlineInfo(ChannelCaseInfoVo vo) {
        String errorMsg = "";
        try {
            log.info("开始同步渠道信息……", vo.getChannelBaseInfo());
            channelOnlineInfoService.saveOrUpdateChannelInfo(vo);
            log.info("########开始同步渠道信息########", vo.getChannelBaseInfo());
        } catch (Exception e) {
            errorMsg = e.getMessage();
            log.error("同步渠道信息失败", vo.getChannelBaseInfo(), e);
        }
        return errorMsg;
    }
}
